package com.example.vue20220225.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.vue20220225.dao.RoleDao;
import com.example.vue20220225.dao.UserDao;
import com.example.vue20220225.model.*;
import com.example.vue20220225.service.RoleService;
import com.example.vue20220225.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    RoleDao roleDao;


    @Override
    public Map<String, Object> add(Role role) {
        Map<String,Object> map = new HashMap<>();
        Integer flag = roleDao.insert(role);
        if(flag < 1){
            map.put("message","新增角色失败");
        }else{
            map.put("message","新增角色成功");
        }
        return map;
    }

    @Override
    public Map<String, Object> editrole(Role role) {
        Map<String,Object> map = new HashMap<>();
        Integer flag = roleDao.updateById(role);
        if(flag < 1){
            map.put("message","修改角色失败");
        }else{
            map.put("message","修改角色成功");
        }
        return map;
    }
    @Override
    @Transactional
    public Map<String, Object> deleterole(Integer roleId) {
        Map<String,Object> map = new HashMap<>();
        Integer flag = 0;
        try {
            // 删除关系表
            roleDao.deleteUserRole(roleId);
            roleDao.deleteRolePower(roleId);
            roleDao.deleteById(roleId);
            flag = 1;
        }catch (Exception e){
            e.printStackTrace();
        }
        if(flag < 1){
            map.put("message","删除角色失败");
        }else{
            map.put("message","删除角色成功");
        }
        return map;
    }

    @Override
    public Map<String, Object> powerselectlist(Integer roleId) {
        Map<String,Object> map = new HashMap<>();
        List<Power> list = roleDao.powerselectlist(roleId);
        List<Power> firlist = new ArrayList<>();// 一级菜单
        List<Integer> firchelist = new ArrayList<>();// 已经选中的一级菜单 只有id
        List<Integer> firalist = new ArrayList<>();// 一级菜单 只有id
        List<Power> seclist = new ArrayList<>();
        List<Integer> secchelist = new ArrayList<>();
        List<Integer> secalist = new ArrayList<>();
        for(Power p : list){
            // 一级菜单
            if(p.getParentId() == 0){
                firlist.add(p);
                firalist.add(p.getPowerId());
                if(p.getStatus() == 1){
                    firchelist.add(p.getPowerId());
                }
            }else{
                // 二级菜单
                seclist.add(p);
                secalist.add(p.getPowerId());
                if(p.getStatus() == 1){
                    secchelist.add(p.getPowerId());
                }
            }
        }
        map.put("firlist",firlist);
        map.put("firalist",firalist);
        map.put("firchelist",firchelist);
        map.put("seclist",seclist);
        map.put("secalist",secalist);
        map.put("secchelist",secchelist);
        if(firlist.size() != 0 && firlist.size() == firchelist.size()){
            map.put("checkfirstAll",true);
        }else {
            map.put("checkfirstAll",false);
            if(firchelist.size() != 0){
                map.put("isIndeterminate",true);
            }else{
                map.put("isIndeterminate",false);
            }
        }
        if(seclist.size() != 0 && seclist.size() == secchelist.size()){
            map.put("checksecondAll",true);
        }else {
            map.put("checksecondAll",false);
            if(firchelist.size() != 0){
                map.put("isIndeterminate2",true);
            }else{
                map.put("isIndeterminate2",false);
            }
        }
        return map;
    }

    @Override
    public Map<String, Object> editpowerist(CheckPower checkPower) {
        Map<String,Object> map = new HashMap<>();
        if(checkPower.getFirstpower().size() < 1 || checkPower.getSecondpower().size() < 1){
            map.put("message","请选择对应的一二级菜单");
            return map;
        }
        Integer flag = 0;
        try {
            // 删除以前的权限菜单
            roleDao.deleteRolePower(checkPower.getRoleId());
            // 新增权限菜单对应关系
            List<Integer> list = new ArrayList<>();
            list.addAll(checkPower.getFirstpower());
            list.addAll(checkPower.getSecondpower());
            roleDao.insertRolePower(list,checkPower.getRoleId());
            flag = 1;
        }catch (Exception e){
            e.printStackTrace();
        }
        if(flag < 1){
            map.put("message","分配权限失败");
        }else{
            map.put("message","分配权限成功");
        }
        return map;
    }

//    @Override
//    public Map<String, Object> powerselectlist(Integer roleId) {
//        Map<String,Object> map = new HashMap<>();
//        List<Power> list = roleDao.powerselectlist(roleId);
//        List<Power> flist = new ArrayList<>();
//        int fnum = 0;
//        int snum = 0;
//        for(Power p : list){
//            // 一级菜单
//            if(p.getParentId() == 0){
//                flist.add(p);
//                if(p.getStatus() == 1){
//                    // 如果该角色有该权限
//                    fnum++;
//                }
//                List<Power> slist = new ArrayList<>();
//                // 二级菜单
//                for(Power p2 : list){
//                    if(p2.getParentId() == p.getPowerId()){
//                        slist.add(p2);
//                    }
//                }
//                p.setList(slist);
//            }
//        }
//        // 一级菜单是否全选
//        if(fnum == flist.size()){
//            map.put("checkfirstAll",true);
//        }else{
//            map.put("checkfirstAll",false);
//        }
//        map.put("powers",flist);
//        return map;
//    }
}
